import Vue from 'vue'
import Router from 'vue-router'
import http from '../utils/http'
import Cookies from 'js-cookie'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

export const constantRoutes =
  [
    {
      path: '/login',
      component: () => import('@/views/login/index'),
      hidden: true
    },


    {
      path: '/404',
      component: () => import('@/views/404'),
      hidden: true
    },
    {
      path: '/666', // 👈 新增的路由
      component: () => import('@/views/404'),
      hidden: true
    },


    // 404 page must be placed at the end !!!
    // { path: '*', redirect: '/404', hidden: true },
     {
      path: '/',
      component: Layout,
      redirect: '/dashboard',
      children: [{
        path: 'dashboard',
        name: '主页',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '主页', icon: 'ri-bar-chart-box-fill' }
      }]
    }

  ]
//动态加载路由，用于用户权限管理
export const initr = {
  iiir() {
    console.log('in to iiir')
    let cr3 = localStorage.getItem('cr3')
    // console.log('cr3:'+cr3)
    if (cr3 == null) {
      // this.hasAdminMenu()
      console.log('没有获取到路由')
    } else {
      let data = JSON.parse(cr3)
      let len = data.length
       console.log(data)
      for (let i = 0; i < len; i++) {

        if (data[i].uri ) {
           // console.log('id:' + data[i].id)
          // console.log('parent_id:' + data[i].parent_id)
          let msg = {}
          // if (data[i].uri == '/system') {
          //    console.log(data[i])
            let children =[]
            for(let s =0;s<data[i].childrens.length;s++){

              if(data[i].childrens[s].childrens.length>0){
                // console.log('in to if');
                let items = {
                  path: data[i].childrens[s].uri,
                  name: data[i].childrens[s].title,
                  component:resolve=>(require([`@/views/${data[i].childrens[s].extension}`],resolve)),
                  meta: { title:  data[i].childrens[s].title, icon: data[i].childrens[s].icon },
                  children:[]
                }
                for(let c=0;c<data[i].childrens[s].childrens.length;c++){
                   // console.log(data[i].childrens[s].childrens[c])
                  if(data[i].childrens[s].childrens[c].childrens.length>0){
                    // console.log(data[i].childrens[s].childrens[c].childrens)
                    let ch = {
                      path: data[i].childrens[s].childrens[c].uri,
                      name: data[i].childrens[s].childrens[c].title,
                      component:resolve=>(require([`@/views/${data[i].childrens[s].childrens[c].extension}`],resolve)),
                      meta: { title:  data[i].childrens[s].childrens[c].title, icon: data[i].childrens[s].childrens[c].icon },
                      children:[]
                    }
                    items.children.push(ch)
                    for(let d=0;d<data[i].childrens[s].childrens[c].childrens.length;d++){
                      let cha = {
                        path: data[i].childrens[s].childrens[c].childrens[d].uri,
                        name: data[i].childrens[s].childrens[c].childrens[d].title,
                        component:resolve=>(require([`@/views/${data[i].childrens[s].childrens[c].childrens[d].extension}`],resolve)),
                        meta: { title:  data[i].childrens[s].childrens[c].childrens[d].title, icon: data[i].childrens[s].childrens[c].childrens[d].icon },
                        children:[]
                      }
                      ch.children.push(cha)
                    }
                  }else{

                    let ch = {
                      path: data[i].childrens[s].childrens[c].uri,
                      name: data[i].childrens[s].childrens[c].title,
                      component:resolve=>(require([`@/views/${data[i].childrens[s].childrens[c].extension}`],resolve)),
                      meta: { title:  data[i].childrens[s].childrens[c].title, icon: data[i].childrens[s].childrens[c].icon },
                      children:[]
                    }
                    items.children.push(ch)
                  }

                }
                children.push(items)

              }else{
                // console.log('in to else');
                let item = {
                  path: data[i].childrens[s].uri,
                  name: data[i].childrens[s].title,
                  component:resolve=>(require([`@/views/${data[i].childrens[s].extension}`],resolve)),
                  meta: { title:  data[i].childrens[s].title, icon: data[i].childrens[s].icon },
                  children:[]
                }
                children.push(item)
              }

            }
            let redirect =""
            if(data[i].childrens.length>0){
              redirect = '/'+ data[i].childrens[0].extension
            }else{
              redirect = '/'+ data[i].extension
            }
            if(data[i].childrens.length>0){
              msg = {
                path: data[i].uri,
                component: Layout,
                redirect: '/'+ data[i].childrens[0].extension,
                meta: {
                  title: data[i].title,
                  icon: data[i].icon
                },
                children:children
              }
            }else{
               // console.log('-------else'+data[i].title);
              msg = {
                path: data[i].uri,
                component: Layout,
                redirect: '/'+ data[i].extension,
                children:[{
                  path: data[i].extension,
                  component:resolve=>(require([`@/views/${data[i].extension}`],resolve)),
                  meta: { title:  data[i].title, icon: data[i].icon },
                }]
              }

              // {
              //   path: '/',
              //     component: Layout,
              //   redirect: '/dashboard',
              //   children: [{
              //   path: 'dashboard',
              //   name: '控制面板',
              //   component: () => import('@/views/dashboard/index'),
              //   meta: { title: '控制面板', icon: 'ri-bar-chart-box-fill' }
              // }]
              // }
            }

            let flag = false
            for(let t=0;t<constantRoutes.length;t++){
               // console.log('-------constantRoutes[t].path'+constantRoutes[t].path);
               // console.log('-------'+msg.path);
              if(constantRoutes[t].path == msg.path){
                flag = true
              }
            }
           // console.log('-------'+flag);
            if(!flag){
              constantRoutes.push(msg)
            }

          // }

        }
      }
    }
     console.log(constantRoutes)
    // console.log('end:')
    router.addRoutes(constantRoutes)
    return constantRoutes
  },
  hasAdminMenu() {
    http.get('/java/AdminMenu/hasAllForJSON', '', (res) => {

      console.log(res)
      let data = res.data
      let len = data.length

      console.log('constantRoutes:')
      console.log(constantRoutes)
      let str = JSON.stringify(data)
      console.log(str)
      // Cookies.set('cr',1,{expires:1});
      // Cookies.set('cr2',str,{expires:1});

      localStorage.setItem('cr3', str)
      // Cookies.set('cr',JSON.stringify(constantRoutes),{expires:1});
      resetRouter()
    })
  }

}

const createRouter = () => new Router({

  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // routes: initr.iiir()
  routes:constantRoutes

})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  console.log('in to resetRouter')
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
  initr.iiir()
}

export default router
